package com.amazon.shop.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.amazon.shop.bean.dto.LoginFrom;
import com.amazon.shop.bean.entity.RelUserStore;
import com.amazon.shop.bean.enums.ResultEnum;
import com.amazon.shop.bean.vo.Ret;
import com.amazon.shop.bean.entity.User;
import com.amazon.shop.exception.BusinessException;
import com.amazon.shop.mapper.UserMapper;
import com.amazon.shop.service.IRelUserStoreService;
import com.amazon.shop.service.IUserService;
import com.amazon.shop.util.ResultUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzj
 * @since 2021-02-26
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private IRelUserStoreService relUserStoreService;

    @Override
    public Ret login(LoginFrom loginFrom, HttpServletRequest request, HttpServletResponse response) {
        String username = loginFrom.getUsername();
        String password = loginFrom.getPassword();
        String encryptPwd = SecureUtil.md5(password);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","nickname","username","email","password","ismanager","disable");
        queryWrapper.eq("username",username);
        User user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            throw new BusinessException(ResultEnum.USERNOTEXIST);
        }else {
            Integer disable = user.getDisable();
            if(disable==1){
                throw new BusinessException(ResultEnum.USERDISABLED);
            }
            String dbpassword = user.getPassword();
            if (!dbpassword.equals(encryptPwd)) {
                throw new BusinessException(ResultEnum.PWDERROR);
            }
        }
        request.getSession().setAttribute("user", user);
        return ResultUtil.success(user);
    }

    @Override
    public Ret logOut(String username, HttpServletRequest request, HttpServletResponse response) {
        request.getSession().removeAttribute("user");
        return ResultUtil.success();
    }

    @Override
    public List<Integer> listStoreidByUserid(String operatorUserid) {
        if (StrUtil.isNotBlank(operatorUserid)) {
            Map parms = new HashMap<>();
            parms.put("userid", Integer.valueOf(operatorUserid));
            List<RelUserStore> list = relUserStoreService.listByMap(parms);
            return list.stream().map(RelUserStore::getStoreid).collect(Collectors.toList());
        }
        return null;
    }

}
